package autoFile.core.strategy;

import autoFile.core.strategy.interfaces.CreateFileStrategyInterface;
import com.google.common.base.CaseFormat;
import org.apache.commons.lang.StringUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-08-16
 * Time: 下午4:19
 * Description: ${description}
 */
public class CreateUserCommandFileStrategy extends AbstractStrategy implements CreateFileStrategyInterface {

    private String classString;
    private String methodString;
    private String voidMethodString;

    @Override
    public void createFile(Class clazz) throws IOException {
        String userCommandTemplate = getUserCommandTemplate();
//        String userCommandMethodTemplate = getUserCommandMethodTemplate();
        String className = clazz.getSimpleName(); //类名称
        String classNameSmall = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL).convert(className); //类名称小写
        String classNameWithService = new String(className); //类名称后面有Service
        String classNameWithServiceSmall = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL).convert(classNameWithService); //类名称小写后面有Service

        if (className.contains("Service")) {
            className = className.replaceFirst("Service", "");
            classNameSmall = classNameSmall.replaceFirst("Service", "");
        }
        className = className.replace("Db", "Command");
        String targetClassName = className + "Proxy";//生成文件名称

        String userCommandMethodTemplates = "";
        List<Method> publicMethod = getPublicMethod(clazz);
        for (Method method : publicMethod) {
            String parameterTypeAndName = getParameterTypeAndName(method);
            String parameterNames = getParameterNames(method);
            String dbConstant = getDbConstant(className, method);
            String userCommandMethodTemplate = getUserCommandMethodTemplate(method);
            String classNameT = userCommandMethodTemplate.replace("className", classNameWithServiceSmall);
            String methodName = classNameT.replace("methodName", method.getName());
            String parameters = methodName.replace("parameters", parameterTypeAndName);
            String parameterNames1 = parameters.replace("parameterNames", parameterNames);
            parameterNames1 = parameterNames1.replace("$", targetClassName);

            String result = parameterNames1.replace("dbConstant", dbConstant);
            result += "\t";
            userCommandMethodTemplates += result;
        }
        String replaceContent = userCommandTemplate.replace("#", userCommandMethodTemplates);
        String replace$ = replaceContent.replace("$", targetClassName);
        String ClassName = replace$.replace("ClassName", classNameWithService);
        String result = ClassName.replace("className", classNameWithServiceSmall);
        result = result.replace("!", ".");
        String fileNamePre = getBasicPath() + "com/gamejelly/gong2/dbs/proxy/";
        String fileName = fileNamePre + targetClassName + ".java";
        FileWriter userCommandProxyWriter = new FileWriter(fileName, false);
        System.out.println("生成" + fileName);
        userCommandProxyWriter.write(result);
        userCommandProxyWriter.close();
    }

    /**
     * 得到dbs模板
     *
     * @return
     */
    private String getUserCommandTemplate() throws IOException {
        if (StringUtils.isNotEmpty(classString)) {
            return classString;
        }
        classString = getTemplate("template/dbs/classTemplate");
        return classString;
//        return getTemplateByFullPath("/Users/liuliwei/autoFile/template/userCommandTemplate");
    }

    /**
     * 得到  dbs  方法模板
     *
     * @return
     * @throws IOException
     */
    private String getUserCommandMethodTemplate() throws IOException {
        if (StringUtils.isNotEmpty(methodString)) {
            return methodString;
        }
        methodString = getTemplate("template/dbs/methodTemplate");
        return methodString;
//        return getTemplateByFullPath("/Users/liuliwei/autoFile/template/userCommandMethodTemplate");
    }

    private String getUserCommandVoidMethodTemplate() throws IOException {
        if (StringUtils.isNotEmpty(voidMethodString)) {
            return voidMethodString;
        }
        voidMethodString = getTemplate("template/dbs/voidMethodTemplate");
        return voidMethodString;
//        return getTemplateByFullPath("/Users/liuliwei/autoFile/template/userCommandVoidMethodTemplate");
    }

    private String getUserCommandMethodTemplate(Method method) throws IOException {
        Class<?> returnType = method.getReturnType();
        if (returnType.equals(Void.TYPE)) {
            return getUserCommandVoidMethodTemplate();
        } else {
            return getUserCommandMethodTemplate();
        }
    }
}
